/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.iqtitles.explorer;


import idea.model.qnrlibrary.QuestionTitleLinkView;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JToolBar;
import org.netbeans.api.print.PrintManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Question Title Top component which displays Question Titles from a given
 * Questionnaire. 
 * Note: This queries the question_title_link_view for a given qnr_id.
 * 
 * @see TopComponent
 * @see ExplorerManager.Provider
 */
@ConvertAsProperties(
dtd = "-//idea.iqtitles.explorer//IQuestionTitle//EN" ,
autostore = false )
@TopComponent.Description(
preferredID = "IQuestionTitleTopComponent" ,
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( position = 2, mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.iqtitles.explorer.IQuestionTitleTopComponent" )
@ActionReferences({
        @ActionReference( path = "Menu/Window" , position = 533  ),
        @ActionReference( path = "Menu/File/Open" , position = 1400 ),
})
@TopComponent.OpenActionRegistration(
displayName = "#CTL_IQuestionTitleAction" ,
preferredID = "IQuestionTitleTopComponent" )
@Messages(
{
    "CTL_IQuestionTitleAction=Question Titles" ,
    "CTL_IQuestionTitleTopComponent=<HTML><B>Question Title Window</B></HTML>" ,
    "HINT_IQuestionTitleTopComponent=This is a Question Title window"
} )
public final class IQuestionTitleTopComponent extends TopComponent implements ExplorerManager.Provider
{
    /**
     * This handles setting up the Nodes in the window and allows them to be associated
     * with the Propertues Window.  In this case the nodes will use an OutlineView.
     */
    private static ExplorerManager exMgr = new ExplorerManager();


    /**
     * simple class constructor
     */
    public IQuestionTitleTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_IQuestionTitleTopComponent() );
        setToolTipText( Bundle.HINT_IQuestionTitleTopComponent() );
        
        // set up print ability
        putClientProperty("print.printable", Boolean.TRUE);
        putClientProperty("print.name", "Ed's Question Titles");

        setLayout( new BorderLayout() );
        initToolbar();
        
        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ) );
    }
    
    /**
     * inits toolbar -- done to avoid leaking "this" in the constructor
     */
    private void initToolbar() {
        JToolBar toolbar = new JToolBar();
        toolbar.addSeparator();
        toolbar.add(PrintManager.printAction(this));
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();
        lblTitleView = new java.awt.Label();

        jScrollPane1.setViewportView(beanTreeView1);

        lblTitleView.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        lblTitleView.setText(org.openide.util.NbBundle.getMessage(IQuestionTitleTopComponent.class, "IQuestionTitleTopComponent.lblTitleView.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitleView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitleView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label lblTitleView;
    // End of variables declaration//GEN-END:variables


    @Override
    public void componentOpened()
    {
        
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    /**
     * gets the explorer manager and
     *
     * @return it
     */
    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }


    /**
     * Display the question titles in this TopComponent using an OutlineView
     *
     * @param resultList returned from the view query
     * @param survey -- for the label
     * @param refPd -- for the label
     * @param formNum -- for the label
     */
    public void displayQuestionTitles( List<QuestionTitleLinkView> resultList, String survey, String refPd, String formNum )
    {
        ExplorerManager exm = getExplorerManager();
        Children kids = Children.create( new IQuestionTitleChildFactory( resultList ) , true );
        Node rootNode = new IQuestionTitleRootNode( kids );

        // creates the OutlineView.  The argument determines the header label for the first (Node) column.
        OutlineView ov = new OutlineView( "Sequence" );

        // sets up the (other) columns and their contents.
        ov.setPropertyColumns(
                //"questSequence" , "Sequence" ,
                "category" , "Category" ,
                "title" , "Question Title" ,
                "version" , "Variation" );

        //sets up the leftmost node column for Double clicking and drilling down.
        Outline o = ov.getOutline();
        o.setRootVisible( false );
        //o.getColumnModel().getColumn( 0 ).setMaxWidth( 30 );
        //o.getColumnModel().getColumn( 0 ).setResizable( false );

        // set up the label at the top of the table
        int size = resultList.size();
        String res = (size == 1 ? size + " result" : size + " results");
        lblTitleView.setText( org.openide.util.NbBundle.getMessage(IQuestionTitleTopComponent.class, "IQuestionTitleTopComponent.lblTitleView.text") 
                + " (for Questionnaire Survey: " + survey + ", " + refPd + ", Form " + formNum + ") -- " + res);
        
        add( lblTitleView , BorderLayout.NORTH );
        add( ov , BorderLayout.CENTER );
        exm.setRootContext( rootNode );
    }

}
